<?php

namespace Superadmins\Controller;

/**
 * 学校编辑
 * @author Administrator
 *
 */
class SchoolController extends CommonController {

  

    /**
     * 搜索页面
     */
    public function index() {
        $type_map = $this->searchMap();
        $SchoolRelation = M('SchoolRelation');
		$School = D('School');
		if (I('get.school_name'))
            $map['school_name'] = array('like', '%' . I('get.school_name') . '%');
		if(I('get.province')||I('get.city')||I('get.college_type')||I('get.school_type')||I('get.nature')||I('get.attribute'))
		{
	        $School_id_data = $SchoolRelation->where($type_map)->getField('school_id', true);
	        $School_id = array_unique($School_id_data);
	        $map['school_id'] = array('in', $School_id);
	    	$count = $School->relation(true)->where($map)->count(); // 查询满足要求的总记录数
	        $Page = new PageController($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
	        $show = $Page->show(); // 分页显示输出
	        $School_data = $School->relation(true)->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
		}
		else 
		{
			$school=M('school');
			$count = $school->where($map)->count(); // 查询满足要求的总记录数
	        $Page = new PageController($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
	        $show = $Page->show(); // 分页显示输出
			$School_data=$school->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
		}
        $SchoolType = D('SchoolType');
        $search_type = $SchoolType->search_type();
        $this->assign('school', $School_data);
        $this->assign('page', $show); // 赋值分页输出		

        $this->assign('search', $search_type);
        $this->display();
    }

    /**
     * 新增或编辑学校页面
     */
    public function editor() {

        //编辑
        if (I('get.school_id')) {
    	
            $school = D('School');
            $map['school_id'] = I('get.school_id');
            $school_data = $school->relation(true)->where($map)->find();

            $SchoolRelation = D('SchoolRelationView');
            $type = $SchoolRelation->where($map)->select();
            $SchoolAdderss = M('SchoolAddress');
            $map['main'] = 1;
            $SchoolAdderss_data = $SchoolAdderss->where($map)->find();

			$list_name=array('departments','info','professional','alumni','address');
  	        
			//获取学校相关信息
  	        foreach($list_name as $val){
            	$this->schoolInfo(I('get.school_id'), $val);
  	        }

            $this->assign('address', $SchoolAdderss_data);
            $this->assign('type', $type);
            $this->assign('school', $school_data);
            $this->assign('condition', 1);
        } else {
        	//新增
        	
            $this->assign('condition', 0);
        }
        $this->assign('schoolType', $this->getAttribute());
        $this->display();
    }

    /**
     * 获取学校相关属性
     */
    public function getAttribute() {
        $SchoolType = D('SchoolType');
        return $SchoolType->search_type();
    }

    /**
     * 根据操作选择新增或删除
     */
    public function schoolAction() { //编辑学校信息
        if (I('post.school_id')) {
            $this->addSchool($this->Image());
            $this->success('修改成功');
        } else { //新增学校信息
            //生成缩略图并插入数据库
            if ($this->addSchool($this->Image())) {

                $this->success('新增成功');
            }
        }
    }

    /**
     * 学校LOGO上传并且进行缩略处理
     * @return 图片地址
     */
    public function Image() {
        $file_info = $this->fileUpload();
        if ($file_info) {
            $file_logo = $file_info['logo'];
            $file_url = 'Uploads/' . $file_logo['savepath'] . $file_logo['savename'];
            $file_name = $file_logo['savename'];

            $image = new \Think\Image();
            $image->open($file_url);
            // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
            $image->thumb(60, 60)->save($file_url);
            return $file_url;
        }
        return null;
    }

    /**
     * 地址编辑
     */
    public function address($school_id) {
        $SchoolType = M('SchoolType');
        $this->GetAddRelationId($school_id, $SchoolType);
    }
    
    
		//新增Type中的地址属性并返回id
    public function addressTypeAdd($school_id, $SchoolType, $address_id, $address_name, $sort) {
        $SchoolRelation = M('SchoolRelation');
        if ($address_id) {
            $data = array(
                'school_id' => $school_id,
                'school_type_id' => $address_id,
            );
            $s = $SchoolRelation->add($data);
        } else {
            $data['school_type_name'] = $address_name;
            $data['school_type_sort'] = $sort;

            $school_type_id = $SchoolType->add($data);
            $SchoolRelationData['school_id'] = $school_id;
            $SchoolRelationData['school_type_id'] = $school_type_id;
            $s = $SchoolRelation->add($SchoolRelationData);
        }
        return $s;
    }

    /**
     * 判断是否有地址属性如果有,插入到连接表,如果没有插入新属性返回的值插入连接表
     * @param unknown_type $school_id
     * @param unknown_type $SchoolType
     */
    public function GetAddRelationId($school_id,$is_address,$s_main) {

    	$SchoolType=M('SchoolType');
        $map['school_type_name'] = I('post.province');

        $province_id = $SchoolType->where($map)->getField('school_type_id');

        unset($map);
        $map['school_type_name'] = I('post.city');
        $city_id = $SchoolType->where($map)->getField('school_type_id');

        unset($map);
        $map['school_type_name'] = I('post.county');
        $county_id = $SchoolType->where($map)->getField('school_type_id');
        $SchoolAddress = M('SchoolAddress');
        $address_data=array(
        		'province_relation_id'=>$this->addressTypeAdd($school_id, $SchoolType, $province_id, I('post.province'), 2),
        		'city_relation_id'=>$this->addressTypeAdd($school_id, $SchoolType, $city_id, I('post.city'), 3),
        		'county_relation_id' => $this->addressTypeAdd($school_id, $SchoolType, $county_id, I('post.county'), 13),
        		'name'=>I('post.name'),
        		'school_address'=>I('post.school_address'),
        		'province'=>I('post.province'),
        		'city'=>I('post.city'),
        		'county'=>I('post.county'),
        		'zip'=>I('post.zip'),
        		'coordinate_x'=>I('post.coordinate_x'),
        		'coordinate_y'=>I('post.coordinate_y'),
        		'school_id'=>$school_id,
        );
        
        
        //判断是否是学校地址的新增地址

        //$is_address0为新增，1为修改
        $address_data['main'] = 0;
        if($is_address==1){ 
	        $map['school_id'] = $school_id;
			$map['main'] = 1;
        }
        if(I('post.main')==1)
        	 $address_data['main'] = 1;
        	 
    	if($s_main)
			{
				unset($map);
				$map['address_id']=$s_main;
			}
		
		$address = $SchoolAddress->where($map)->find();
        if ($address && $is_address!=0) {
            $map['address_id'] = $address['address_id'];

            $id=$SchoolAddress->where($map)->save($address_data);
        } else {
            $id=$SchoolAddress->add($address_data);
        }
    	return $id;
    }

    /**
     * 新增学校与编辑操作
     */
    public function addSchool($file_url) {

	
        $data = array(
            'school_name' => I('post.school_name'),
            'aliases' => I('post.aliases'),
            'school_time_year' => I('post.school_time_year'),
            'website' => I('post.website'),
        );

        if ($file_url)
            $data['logo'] = $file_url;

        $school = M('school');
        if (I('post.school_id')) {
        	
        	$school_id = I('post.school_id');
            $data['school_id'] = $school_id;
       		$school->save($data);
            
        } else
            $school_id = $school->add($data);

        if ($school_id) {
            //获取学校属性信息
				
            $schoolTypeId = I('post.schoolTypeId');
           
            if (isset($schoolTypeId)) {

                //组装并且一次性插入数据
                foreach ($schoolTypeId as $v) {
                    $array[] = array(
                        'school_id' => $school_id,
                        'school_type_id' => $v,
                    );
                }

                $SchoolRelation = M('SchoolRelation');
                $SchoolRelation->where('school_id=' . $school_id)->delete();
                $SchoolRelation->addAll($array);

        		$this->GetAddRelationId($school_id,  M('SchoolType'));
            }
        }
        return $school_id;
        
        
    }

    /**
     * 获取学校相关信息
     */
    public function schoolInfo($school_id, $model_name) {
        $map['school_id'] = $school_id;
		$model_name_b='school_'.$model_name;
        $model = M($model_name_b);
        if ($model_name_b == 'school_info')
            $data = $model->where($map)->find();
        else
            $data = $model->where($map)->order($model_name.'_id desc')->select();
		
        $this->assign($model_name_b, $data);
    }

    /**
     * AJAX方式编辑学校相关信息
     */
    public function editorSchoolInfo() {
      $action = I('param.action');
        if($action!='school_address'){
        	$model = M($action);
	      	foreach (I('param.') as $k => $v) {
	      		if ($k != 'action')
	            	$data[$k] = $v;
	        }
                if(empty($data["info_id"])){
                    unset($data["info_id"]);
                    if($model->add($data));
                    $this->ajaxReturn(I('param'));
                }else{
                   if ($model->save($data))
	              $this->ajaxReturn(I('param')); 
                } 
        }else{
        	if(I('param.main')!=1){
        		
	        	$params['school_id']=I('param.school_id');
	        	$this->GetAddRelationId($params['school_id'],1,I('param.address_id'));
	        	$this->redirect('School/editor',$params);
        	}else{
        		$school_address = M('school_address');
        		$map['school_id']=I('param.school_id');
        		$data['main']=0;
        		$school_address->where($map)->save($data);
        		$map['address_id']=I('param.address_id');
        		$data['main']=1;
        		$school_address->where($map)->save($data);
        	}
        }
    }

    /**
     * AJAX方式新增学校相关信息
     */
    public function addSchoolInfo() {
        $action = I('param.action');
      	//除了学校地址的新增
        if($action!='school_address'){
	        $model = M($action);
	      	foreach (I('param.') as $k => $v) {
	      		if ($k != 'action')
	            	$data[$k] = $v;
	        }
	         if ($id=$model->add($data))
	            $this->ajaxReturn($id);
        }else{
        	$params['school_id']=I('param.school_id');
        	$this->GetAddRelationId($params['school_id'],0);
        	$this->redirect('School/editor',$params);
        }
    }

    /**
     * AJAX方式删除学校相关信息
     */
    public function deleteSchoolInfo() {
        $action = I('param.action');
        $model = M($action);
    	foreach (I('param.') as $k => $v) {
            if ($k != 'action')
                $sid = $v;
        }
        if ($model->delete($sid))
            $this->ajaxReturn(I('param.'));
    }

    /**
     * 高校图库
     */
    public function school_img() {

        $this->display();
    }

    /**
     * 点评管理
     */
    public function comment() {
        $SchoolComment = M('SchoolComment');
        $school_id = I("get.school_id");
        $map['school_id'] = $school_id;
        $map['status'] = 0;
        $count = $SchoolComment->where($map)->count(); // 查询满足要求的总记录数
        $Page = new PageController($count, 6); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $School_data = $SchoolComment->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
     
        $this->assign('comment', $School_data);
        $this->assign('page', $show); // 赋值分页输出	
        $this->display();
    }

    /*
     * 查看点评功能 
     * 
     *   */

    public function commentselect() {
       
        $comment_id = I("post.comment_id");
        $map['comment_id'] = $comment_id;
        $SchoolComment = M('SchoolComment');
        $comment = $SchoolComment->where($map)->find();
//        echo $SchoolComment->_Sql();
//        print_r($comment);
        $this->ajaxReturn($comment);
    }

    /*
     * 删除点评 
     * 假删除把status状态该为1
     *          */

    public function commentsdelete() {
       
//        $school_id = I("get.school_id");
        $comment_id = I("post.comment_id");
//        $map['school_id'] = $school_id;
        $map['comment_id'] = $comment_id;
        $data['status'] = 1;
        $SchoolComment = M('SchoolComment');

        if (($SchoolComment->where($map)->save($data)) > 0) {
             $this->ajaxReturn($comment_id);
        } else {
             $this->ajaxReturn($comment_id);
        }
    }
    

    public function editorcomments(){
         $school_id = I('get.school_id');
         $SchoolComment = M('SchoolComment');
         $data = I("post.");
         $SchoolComment->where($map)->save($data);
         $this->success("修改成功");
    }

    /**
     * 报名管理
     */
    public function apply() {
        $SchoolApply = M('SchoolApply');
        $school_id = I("get.school_id");
        $map['school_id'] = $school_id;
        $count = $SchoolApply->where($map)->count(); // 查询满足要求的总记录数
        $Page = new PageController($count, 6); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $School_data = $SchoolApply->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('apply', $School_data);
        $this->assign('school_id', $school_id);
        $this->assign('page', $show); // 赋值分页输出	
        $this->display();
    }
    
    Public function getOneApply(){
    	$data=I("post.");
    	if($data['apply_id']){
    		$map['apply_id']=$data['apply_id'];
    		$SchoolApply = M('SchoolApply');
    		$data=$SchoolApply->where($map)->find();
    	}
    	$this->ajaxReturn($data);
    }
    
	Public function deleteApply(){
    	$data=I("post.");
    	if($data['apply_id']){
    		$map['apply_id']=$data['apply_id'];
    		$SchoolApply = M('SchoolApply');
    		$flag=$SchoolApply->where($map)->delete();
    	}
    }

}
